package com.heartrate.module.chart;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.FrameLayout;

import com.heartrate.R;
import com.heartrate.module.base.BaseActivity;
import com.jonas.jgraph.graph.JcoolGraph;
import com.jonas.jgraph.models.Jchart;

import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by admin on 2017/8/10.
 */

public class HeartChart extends BaseActivity implements CompoundButton.OnCheckedChangeListener{

    @BindView(R.id.sug_recode_line)
    JcoolGraph mLineChar;
    private float mMaxHeart;
    private float mMinHeart;
    private int chartNum = 24;
    private String[] mXmsg = {"00::00","06:00","12:00","18:00","24:00"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_heart_chart);
        ButterKnife.bind(this);
        ( (CheckBox)findViewById(R.id.ymsg) ).setOnCheckedChangeListener(this);
        initLines();

    }

    private void initLines(){
        List<Jchart> lines = new ArrayList<>();
        int j = 0 ;
        for(int i = 0; i<chartNum; i++) {

            //lines.add(new Jchart(new SecureRandom().nextInt(50)+15, Color.parseColor("#5F77F6")));
            //设置x轴坐标 为 "00::00","06:00","12:00","18:00","24:00" 一天24小时
            if (i == 0 || (i+1)%6 == 0){
                lines.add(new Jchart(40,new SecureRandom().nextInt(100)+60,mXmsg[j], Color.parseColor("#b8e986")));
                j++;
            }else{
                lines.add(new Jchart(40,new SecureRandom().nextInt(100)+60," ", Color.parseColor("#b8e986")));
            }

        }
        float fNum = 0;
        for (int i = 0; i < lines.size() -1;i++){
            fNum  +=  lines.get(i).getNum();
        }
        float fAvage = fNum /lines.size();
        System.out.println("最下值========"+fAvage);
        mMaxHeart = Collections.max(lines, new Comparator<Jchart>() {
            @Override
            public int compare(Jchart lhs, Jchart rhs){
                return (int)( lhs.getTopest()-rhs.getTopest() );
            }
        }).getTopest();
        mMinHeart = Collections.min(lines, new Comparator<Jchart>() {
            @Override
            public int compare(Jchart lhs, Jchart rhs){
                return (int)( lhs.getLowest()-rhs.getLowest() );
            }
        }).getLowest();
        mLineChar.setLineShowStyle(3);
        mLineChar.setShaderAreaColors(Color.parseColor("#4B494B"), Color.TRANSPARENT);
        mLineChar.setPaintShaderColors(Color.RED, Color.parseColor("#E79D23"),
                Color.parseColor("#FFF03D"), Color.parseColor("#A9E16F"), Color.parseColor("#75B9EF"));

        String[] mYsg = {"40","100","160"};
        mLineChar.setYaxisValues(mYsg);
        mLineChar.setLinePointRadio((int)mLineChar.getLineWidth());
        mLineChar.setNormalColor(Color.parseColor("#676567"));
        mLineChar.feedData(lines);
    }

    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
        switch (compoundButton.getId()){
            case R.id.ymsg:
//                if(isChecked) {
//                    mLineChar.setYaxisValues(30, 100, 5);
//                }else {
//                    mLineChar.setYaxisValues(0, 0, 0);
//                }
                break;
        }
        mLineChar.postInvalidate();
    }
}
